<%@ page language="java" contentType="text/html; charset=windows-1255"
    pageEncoding="windows-1255"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link href="public/style.css" rel="stylesheet" type="text/css"/>
<%@page import="java.io.File"%>
<%@page import="BusinessLogic.Result.ResultType"%>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.LinkedList" %>
<%@ page import="BusinessLogic.Result" %>
<%@ page import="BusinessLogic.Session" %>
<%@ page import="BusinessLogic.CoursesContainer" %>
<%@ page import="BusinessLogic.Course" %>
<%@page import="BusinessLogic.User"%>
<title>Easy Schedule</title>
<script type="text/javascript" src="public/jquery.js"></script>
<script type="text/javascript">

	function replaceSpaces(name){
		var s = name.replace(/\s/g,"1");
		return s;
	}
	function showCourseDetails(cName){
		$('#'+replaceSpaces(cName)).toggleClass('adminkey');
	}

	function deleteCourse(admin,cName){
		var answer = confirm ("Are you sure you want to delete the course: "+cName+"?");
		if (answer)
			{
			$('#div'+replaceSpaces(cName)).remove();
			$.get(
				    "/tamirandevyatar/requestHandler",
				    { action: "deleteCourse", NAME: cName ,ADMIN : admin},
				    function(data) { data=data+''; if (data.indexOf("success",0)<0) alert(data); },
				    "html"
				);
			}
	}
	function editCourse(admin,cName){
		window.location.href = "./edit?NAME="+cName+'&'+"ADMIN="+admin;
	}
	function registerCourse(me,name)
	{
		$.get(
			    "/tamirandevyatar/requestHandler",
			    { action: "register", USER_NAME: me ,COURSE : name},
			    function(data) { data=data+''; if (data.indexOf("success",0)<0) alert(data); 
			    location.reload(true);},
			    "html"
			);
	}
	function unregisterCourse(me,name)
	{
		$.get(
			    "/tamirandevyatar/requestHandler",
			    { action: "unregister", USER_NAME: me ,COURSE : name},
			    function(data) { data=data+''; if (data.indexOf("success",0)<0) alert(data); 
			    location.reload(true);},
			    "html"
			);
	}
	function onlyAvailable()
	{
		$("div.av").toggle();
	}
</script>
<jsp:useBean id="requestHandler" class="RequestHandlerPackage.RequestHandler" scope="application"/>
<%
String uName = request.getUserPrincipal().getName();
Map<String, String[]> params = new HashMap<String,String[]>();
params.put("action",new String[]{"getAllCourses"});
Result res = requestHandler.handle(params);
CoursesContainer cc = null;
List<Course> courses = null;
if (res.getResultType().equals(ResultType.FAILURE)){
  	String adminUrl = request.getContextPath()+"/admin.jsp";	
	%>
	<script type="text/javascript">
	alert("The DB encountered a problem, please try again.");
	</script>
	<meta http-equiv="Refresh" content="5; url=<%=adminUrl %>"/>
	<% 
}else{
  cc = (CoursesContainer)res.getSuccessReturnValue();
  courses= cc.getCourses();
}
//check admin
params.clear();
params.put("action",new String[]{"getUser"});
params.put("USER_NAME",new String[]{uName});
params.put("me",new String[]{uName});
res = requestHandler.handle(params);
boolean admin = false;
if (res.getResultType().equals(ResultType.SUCCESS))
{
	if (((User)(res.getSuccessReturnValue())).getRole().equals("admin")){
	  	admin = true;
	}
}
List<Course> available = new LinkedList<Course>();
List<Course> registered = new LinkedList<Course>();
if (!admin)
{
	params.clear();
	params.put("action",new String[]{"availableCourses"});
	params.put("USER_NAME",new String[]{uName});
	res = requestHandler.handle(params);
	if (res.getResultType().equals(ResultType.SUCCESS))
	{
		available = (List<Course>)res.getSuccessReturnValue();
	}
	params.clear();
	params.put("action",new String[]{"getTimetable"});
	params.put("USER",new String[]{uName});
	res = requestHandler.handle(params);
	if (res.getResultType().equals(ResultType.SUCCESS))
	{
		CoursesContainer reg = (CoursesContainer)res.getSuccessReturnValue();
		registered = reg.getCourses();
	}
}
%>

</head>
<body>
	    <div id="page">
	        <div id="header">
	            <h1><a href="./">Easy<span class="logo">Schedule</span></a></h1>
	            <p id="subtitle">Nice and easy registration system</p>
	        </div>
	        <div id="menu">
	            <ul>
	                <li class="left_menu"><a href="./">Home</a></li>
	                <li class="left_menu"><a href="./about.html">About</a></li>
	                <li class="left_menu"><a href="/tamirandevyatar/logout">Logout</a></li>
	            </ul>
	        </div>
	        <div id="main">	
	            <div id="sidebar">
	                <div class="box" id="sidebar_head">
	                   <%if (admin){ %>
	                    <h2><a id="loginAnchor">Admin</a></h2>
	                     <%}else{ %>
	                      <h2><a id="loginAnchor">User</a></h2>
	                     <%} %>
	                    <div class="box2">
                        <ul>
                        	<%if (admin){ %>
                            <li><a href="/tamirandevyatar/allCourses">Show courses</a></li>
                            <li><a href="/tamirandevyatar/addCourse">Add course</a></li>
                            <li><a href="/tamirandevyatar/users">Manage users</a></li>
                            <li><a href="/tamirandevyatar/update">Update my details</a></li>
                            <%}else{ %>
                            <li><a href="/tamirandevyatar/allCourses">Show courses</a></li>
                            <li><a href="/tamirandevyatar/search">Search courses</a></li>
                            <li><a href="/tamirandevyatar/timetable">My timetable</a></li>
                            <li><a href="/tamirandevyatar/update">Update my details</a></li>
                            <li><a href="/tamirandevyatar/users">View users</a></li>
                            <%} %>
	                        </ul>
	                    </div>
	                </div>		
	            </div><!-- sidebar -->    	              
	            <div id="content">
	                <div id="contheader">
	                    <h2>Welcome!</h2>
	                </div>		
	                <div id="content2" class="welcome">
	                <%if (!admin) { %>
						<input type="checkbox" name="available" value="available" onclick="onlyAvailable()"/>Show Only registration available courses
						<%} for (Course c : courses){
							String courseName = c.getName();
							String divCourse = "\"div"+c.getNameNoSpaces()+"\"";
							String clas = "";
							if (!available.contains(c))
								clas = "class=\"av\"";
							%>
							<div id=<%= divCourse+" "+clas%> >
								<table>
									<tr>
									<td>
									<h4 class="course"><a  class="course" onclick="showCourseDetails('<%=courseName %>')"><%=courseName%></a></h4>
									</td>
									<%String me = request.getUserPrincipal().getName(); 
									  if (me.equals(c.getAdmin())){%>
									  <td>
									  	<br/>
										<button type="button" onclick="deleteCourse('<%=me%>','<%=courseName%>')">delete course</button>
									  </td>
									  <td>
									  	<br/>
										<button type="button" onclick="editCourse('<%=me%>','<%=courseName%>')">edit course</button>
									  </td>
									  <%
									  }
									  if (available.contains(c))
									  {
									  %>
									  <td>
									  	<br/>
									  	<button type="button" onclick="registerCourse('<%=me%>','<%=courseName%>')">Register!</button>
									  </td>
									  <%} 
									  if (registered.contains(c))
									  {
									  %>
									  <td>
									  	<br/>
									  	<button type="button" onclick="unregisterCourse('<%=me%>','<%=courseName%>')">Unregister!</button>
									  </td>
									  <%}%>
									  </tr>
								</table>
								<div class="adminkey" id="<%=c.getNameNoSpaces()%>">
									<p><span class="red">Description:</span> <%=c.getDescription()%></p>
									<p><span class="red">Capacity:</span> <%=c.getCapacity()%></p>
									<p><span class="red">Group:</span> <%=c.getGroup()%></p>
									<p><span class="red">Credit points:</span> <%=c.getPoints()%></p>
									<h4 >Sessions:</h4>
									<% List<Session> sessions = cc.getSessionsOfCourse(courseName);
									for (Session s : sessions){%>
									  <p><span class="red">day:</span> <%=s.getDay()%></p>
									  <p><span class="red">Start time:</span> <%=s.getStart_time()+" :00" %></p>
									  <p><span class="red">End time:</span> <%=s.getEnd_time()+" :00" %></p>
									<%}%>
								</div>
							</div>
						<%}%>
	                </div><!-- content2 -->								              
	            </div><!-- content -->                    
	            <div class="clearing">&nbsp;</div> 
	            <div id="footer">
	                <p><a href="http://www.cs.technion.ac.il/~cs236369/">Managing Data on the World-Wide Web</a></p>
	            </div>
	        </div><!-- main -->
	    </div><!-- page -->
</body>
</html>